<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: User.php 197 2011-02-18 12:45:33Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");

//class Permission extends Baogg_Db_Table {
class Microblog_Mention  extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name;
	protected $_db ;
	protected $_primary; 
	
	function __construct() {
		$this->_name =   'microblog_mention';
		
		$this->primary= $this->_primary='Microblog_mention_id';
		parent::__construct();
	}
	
	function request($url, $type = 'get', $data = array(), $withcookie = 0,$cookiepath='cookie.txt')
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (R1 1.5); InfoPath.2)');
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		if($withcookie)
		{
			//curl_setopt($curl, CURLOPT_COOKIESESSION,true);
			if($withcookie == 2)
				curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiepath);
			else
				curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiepath);
		}
		if(strtolower($type) == 'post')
		{
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
		}
		curl_setopt($curl, CURLOPT_URL, $url);
		$data= curl_exec($curl);
		curl_close($curl);
		return $data;
	}
	
	function handleSearchPages($file_content='',$key_word=''){
		
		$ret=array();
		//echo $file_content;exit;
		$doc=phpQuery::newDocumentHTML($file_content);
		$Mention = new Zend_Db_Table('mention');
	
	
		$result=pq('div.MIB_feed_c > p.sms');
	
		$rows=array();
		$flag=false;
		foreach($result as $k=>$result_v){
			$flag=true;
			$doc=phpQuery::newDocumentHTML($file_content);
			$blog_id=$result_v->getAttribute('mid');	
			
			$text=$result_v->nodeValue;
			
			$uid=$result_v->getElementsByTagName('a')->item(0)->getAttribute('uid');
			
			$blog_url='';
			$result_blog=pq("#mid_{$blog_id} div.lf a:first");
			foreach($result_blog as $v_blog){
				$blog_url=$v_blog->getAttribute('href');			
				break;
			}
			
			$comment_count=0;
			$result_comment_count=pq('strong[rid='.$blog_id.'][type=commtCount]');
			foreach($result_comment_count as $v_comment_count){
				$comment_count=(int)str_replace(array('(',')','（','）'),array('','','',''),$v_comment_count->nodeValue);			
				break;
			}
			
			$repost_count=0;		
			$result_repost_count=pq('#num_'.$blog_id);
			foreach($result_repost_count as $v_repost_count){
				$repost_count=(int)str_replace(array('(',')','（','）'),array('','','',''),$v_repost_count->nodeValue);
				break;
			}
			
			$repost_text='';
			$result_repost_text=pq("#mid_{$blog_id} .MIB_assign_c");
			foreach($result_repost_text as $v_repost_text){
				$repost_text=$v_repost_text->nodeValue;			
				break;
			}
			
			$created_at=0;		
			$result_created_at=pq("#mid_{$blog_id} strong[date]");
			foreach($result_created_at as $v_created_at){
				$created_at=$v_created_at->getAttribute('date');
				break;
			}
			
			$row=array(
				'mention_id'=>$blog_id,
				'mention_text'=>$text,
				'Microblog_user_id'=>$uid,
				'url'=>$blog_url,
				'repost_count'=>$repost_count,
				'comment_count'=>$comment_count,
				'repost_text'=>$repost_text,
				'created_at'=>$created_at,
				'key_word'=>$key_word
			);
			$select=$this->getList(array('mention_id='.((int)$blog_id)));
			//$rs=!$db->query($select)->fetchAll();
			//var_dump($rs);exit;
			if(!$this->_db->query($select)->fetchAll()){
				try{
					$Mention->insert($row);
					handleUserPage($uid);
				}catch(Exception $e){
					$ret[]=$row;				
				}
			}
			
		}
		if($flag==false){
			return false;
		}
		return $ret;
	}

}
